import { isEmpty } from 'lodash';
/**
 * 语言包替换全局的特殊字符
 * @param {*} locales 语言包JSON数据
 * @param {*} globalReplaceConfig 需要替换的配置,数据格式：[{oldStr: '需要被替换的字符串',newStr:'替换的字符串'}]
 * @returns 
 */
const langTextReplace = (locales, globalReplaceConfig) => {
    if (isEmpty(globalReplaceConfig)) { // 没有需要替换的字符串
        return locales;
    }
    const _locales = { ...locales };
    for (let key in _locales) {
        let value = _locales[key];
        if (typeof value === 'object') {
            _locales[key] = langTextReplace(value, globalReplaceConfig); // 递归遍历所有的对象
        } else {
            for (let config of globalReplaceConfig) {
                const reg = new RegExp(config['oldStr'], 'g');
                value = value.replace(reg, config['newStr']);
            }
            _locales[key] = value;
        }
    }
    return _locales;
};

export default langTextReplace;